package com.example.controller;

import com.example.common.Result;
import com.example.entity.Category;
import com.example.service.CategoryService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;
    /**
     * 查询所有分类
     * @return
     */
    @GetMapping("/selectAll")
    public Result selectAll(Category category) {
        List<Category> categories = categoryService.selectAll(category);
        return Result.success(categories);
    }

    /**
     * 分页查询分类
     * @param pageNum
     * @param pageSize
     * @return 分页结果
     */
    @GetMapping("/selectByPage")
    public Result selectByPage(Category category, @RequestParam(defaultValue = "1") Integer pageNum,
                               @RequestParam(defaultValue = "10") Integer pageSize) {
        PageInfo<Category> categoryPageInfo = categoryService.selectByPage(category, pageNum, pageSize);
        return Result.success(categoryPageInfo);
    }

    /**
     * 新增分类
     * @param category
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestBody Category category) {
        categoryService.add(category);
        return Result.success();
    }

    /**
     * 修改分类
     * @param category
     * @return
     */
    @PutMapping("/update")
    public Result update(@RequestBody Category category) {
        categoryService.update(category);
        return Result.success();
    }

    /**
     * 删除分类
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable("id") Integer id) {
        categoryService.delete(id);
        return Result.success();
    }
}
